﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using ThreadUtilHelpV2;

namespace ThreadUtilHelpV2Plus
{
    public static class ThreadToolV2Plus
    {
        public static void SetCtrlTextTF<T>(this T t, string txt) where T : Control
        {
            try
            {
                t.UIThread(delegate()
                {
                    t.Text = txt;
                });
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("SetCtrlTextTF Exception:{0}", ex.Message.Trim()));
            }
        }
        public static void SetCtrlEnableTF<T>(this T t, bool enable) where T : Control
        {
            try
            {
                t.UIThread(delegate()
                {
                    t.Enabled = enable;
                });
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("SetCtrlEnableTF Exception:{0}", ex.Message.Trim()));
            }
        }
        public static void SetCtrlVisibleTF<T>(this T t, bool visible) where T : Control
        {
            try
            {
                t.UIThread(delegate()
                {
                    t.Visible = visible;
                });
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("SetCtrlVisibleTF Exception:{0}", ex.Message.Trim()));
            }
        }
        public static void SetCheckBoxTF(this CheckBox ck, bool check)
        {
            try
            {
                ck.UIThread(delegate()
                {
                    ck.Checked = check;
                });
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("SetCheckBoxTF Exception:{0}", ex.Message.Trim()));
            }

        }
        public static void OpComboBoxTF(this ComboBox cb, int index)
        {
            try
            {
                cb.UIThread(delegate()
                {
                    cb.SelectedIndex = index;
                });
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("OpComboBoxTF Exception:{0}", ex.Message.Trim()));
            }

        }
        public static void AddItemTF(this ListBox ls, object item)
        {
            try
            {
                ls.UIThread(delegate()
                {
                    ls.Items.Add(item);
                });
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("AddItemTF Exception:{0}", ex.Message.Trim()));
            }
        }
        public static void AddItemTF(this ListBox ls, string item)
        {
            try
            {
                ls.UIThread(delegate()
                {
                    ls.Items.Add(item);
                });
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("AddItemTF Exception:{0}", ex.Message.Trim()));
            }
        }
        public static void AddItemTF(this ListBox ls, string item, bool selectedIndex)
        {
            try
            {
                ls.UIThread(delegate()
                {
                    ls.Items.Add(item);
                    if (selectedIndex)
                        ls.SelectedIndex = ls.Items.Count - 1;
                });
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("AddItemTF Exception:{0}", ex.Message.Trim()));
            }
        }
        public static void AddItemTF(this ListBox ls, object item, bool selectedIndex)
        {
            try
            {
                ls.UIThread(delegate()
                {
                    ls.Items.Add(item);
                    if (selectedIndex)
                        ls.SelectedIndex = ls.Items.Count - 1;
                });
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("AddItemTF Exception:{0}", ex.Message.Trim()));
            }
        }
        public static void OpComboBoxTF(this ComboBox cb, int index, bool enable)
        {
            try
            {
                cb.UIThread(delegate()
                {
                    cb.SelectedIndex = index;
                    cb.Enabled = enable;
                });
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("OpComboBoxTF Exception:{0}", ex.Message.Trim()));
            }

        }
        public static string GetTextValueTF(this TextBox txtbox)
        {
            try
            {
                string _txtValue = string.Empty;
                txtbox.UIThread(delegate()
                {
                    _txtValue = txtbox.Text.Trim();
                });
                return _txtValue;
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("GetTextValueTF Exception:{0}", ex.Message.Trim()));
            }
        }
        public static int GetSelectedIndexTF(this ComboBox combox)
        {
            try
            {
                int _selectedIndex = -1;
                combox.UIThread(delegate()
                {
                    _selectedIndex = combox.SelectedIndex;
                });
                return _selectedIndex;
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("GetSelectedIndexTF Exception:{0}", ex.Message.Trim()));
            }
        }
        public static void OpNumericUpDownTF(this NumericUpDown updown, decimal value)
        {
            try
            {
                updown.UIThread(delegate()
                {
                    updown.Value = value;
                });
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("OpNumericUpDownTF Exception:{0}", ex.Message.Trim()));
            }
        }
    }
}
